﻿using System;
using System.Runtime.InteropServices;

namespace GLDotNet
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Matrix4
	{
        public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Matrix4));

		public static readonly Matrix4 Zero = new Matrix4();

		public static readonly Matrix4 Identity = new Matrix4() { M11 = 1.0f, M22 = 1.0f, M33 = 1.0f, M44 = 1.0f };

		public float M11;
        public float M12;
        public float M13;
        public float M14;
		public float M21;
        public float M22;
        public float M23;
        public float M24;
        public float M31;
        public float M32;
        public float M33;
        public float M34;
		public float M41;
        public float M42;
        public float M43;
        public float M44;

		public Matrix4(float m11, float m12, float m13, float m14,
		                float m21, float m22, float m23, float m24,
		                float m31, float m32, float m33, float m34,
			            float m41, float m42, float m43, float m44)
		{
			this.M11 = m11;
			this.M12 = m12;
			this.M13 = m13;
			this.M14 = m14;
			this.M21 = m21;
			this.M22 = m22;
			this.M23 = m23;
			this.M24 = m24;
			this.M31 = m31;
			this.M32 = m32;
			this.M33 = m33;
			this.M34 = m34;
			this.M41 = m41;
			this.M42 = m42;
			this.M43 = m43;
			this.M44 = m44;
		}

		public static Matrix4 CreateRotationX(float radians)
		{
            Matrix4 result;
            CreateRotationX(radians, out result);
            return result;
		}

		public static void CreateRotationX(float radians, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M22 = (float)Math.Cos(radians);
			result.M23 = (float)Math.Sin(radians);
			result.M32 = -result.M23;
			result.M33 = result.M22;
		}

		public static Matrix4 CreateRotationY(float radians)
		{
            Matrix4 result;
            CreateRotationY(radians, out result);
            return result;
		}

		public static void CreateRotationY(float radians, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M11 = (float)Math.Cos(radians);
			result.M13 = (float)-Math.Sin(radians);
			result.M31 = -result.M13;
			result.M33 = result.M11;
		}

		public static Matrix4 CreateRotationZ(float radians)
		{            
			Matrix4 result;
            CreateRotationZ(radians, out result);			
			return result;
		}

		public static void CreateRotationZ(float radians, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M11 = (float)Math.Cos(radians);
			result.M12 = (float)-Math.Sin(radians);
			result.M21 = -result.M12;
			result.M22 = result.M11;
		}

		public static Matrix4 CreateScale(float scale)
		{
            Matrix4 result;
            CreateScale(scale, out result);
            return result;
		}

		public static void CreateScale(float scale, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M11 = scale;
			result.M22 = scale;
			result.M33 = scale;
		}

        public static Matrix4 CreateScale(float x, float y)
        {
            Matrix4 result;
            CreateScale(x, y, out result);
            return result;
        }

        public static void CreateScale(float x, float y, out Matrix4 result)
        {
            result = Matrix4.Identity;

            result.M11 = x;
            result.M22 = y;
        }

		public static Matrix4 CreateScale(float x, float y, float z)
		{
            Matrix4 result;
            CreateScale(x, y, z, out result);
            return result;
		}

		public static void CreateScale(float x, float y, float z, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M11 = x;
			result.M22 = y;
			result.M33 = z;
		}

        public static Matrix4 CreateScale(Vector2 scale)
        {
            Matrix4 result;
            CreateScale(ref scale, out result);
            return result;
        }

        public static void CreateScale(ref Vector2 scale, out Matrix4 result)
        {
            result = Matrix4.Identity;

            result.M11 = scale.X;
            result.M22 = scale.Y;
        }

        public static Matrix4 CreateScale(Vector3 scale)
        {
            Matrix4 result;
            CreateScale(ref scale, out result);
            return result;
        }

        public static void CreateScale(ref Vector3 scale, out Matrix4 result)
        {
            result = Matrix4.Identity;

            result.M11 = scale.X;
            result.M22 = scale.Y;
            result.M33 = scale.Z;
        }

		public static Matrix4 CreateTranslation(float x, float y)
		{
            Matrix4 result;
            CreateTranslation(x, y, out result);
            return result;
		}

		public static void CreateTranslation(float x, float y, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M41 = x;
			result.M42 = y;
		}

		public static Matrix4 CreateTranslation(float x, float y, float z)
		{
            Matrix4 result;
            CreateTranslation(x, y, z, out result);
            return result;
		}

		public static void CreateTranslation(float x, float y, float z, out Matrix4 result)
		{
			result = Matrix4.Identity;

			result.M41 = x;
			result.M42 = y;
			result.M43 = z;
		}

        public static Matrix4 CreateTranslation(Vector2 position)
        {
            Matrix4 result;
            CreateTranslation(ref position, out result);
            return result;
        }

        public static void CreateTranslation(ref Vector2 position, out Matrix4 result)
        {
            result = Matrix4.Identity;

            result.M41 = position.X;
            result.M42 = position.Y;
        }

        public static Matrix4 CreateTranslation(Vector3 position)
        {
            Matrix4 result;
            CreateTranslation(ref position, out result);
            return result;
        }

        public static void CreateTranslation(ref Vector3 position, out Matrix4 result)
        {
            result = Matrix4.Identity;

            result.M41 = position.X;
            result.M42 = position.Y;
            result.M43 = position.Z;
        }

        /// <summary>
        /// Determines the product of two matrices.
        /// </summary>
        /// <param name="left">The first matrix to multiply.</param>
        /// <param name="right">The second matrix to multiply.</param>
        /// <returns>The product of the two matrices.</returns>
        public static Matrix4 Multiply(Matrix4 left, Matrix4 right)
        {
            Matrix4 result;
            Multiply(ref left, ref right, out result);
            return result;
        }

		/// <summary>
		/// Determines the product of two matrices.
		/// </summary>
		/// <param name="left">The first matrix to multiply.</param>
		/// <param name="right">The second matrix to multiply.</param>
		/// <param name="result">The product of the two matrices.</param>
		public static void Multiply(ref Matrix4 left, ref Matrix4 right, out Matrix4 result)
		{
            Matrix4 temp = new Matrix4();
            temp.M11 = (left.M11 * right.M11) + (left.M12 * right.M21) + (left.M13 * right.M31) + (left.M14 * right.M41);
            temp.M12 = (left.M11 * right.M12) + (left.M12 * right.M22) + (left.M13 * right.M32) + (left.M14 * right.M42);
            temp.M13 = (left.M11 * right.M13) + (left.M12 * right.M23) + (left.M13 * right.M33) + (left.M14 * right.M43);
            temp.M14 = (left.M11 * right.M14) + (left.M12 * right.M24) + (left.M13 * right.M34) + (left.M14 * right.M44);
            temp.M21 = (left.M21 * right.M11) + (left.M22 * right.M21) + (left.M23 * right.M31) + (left.M24 * right.M41);
            temp.M22 = (left.M21 * right.M12) + (left.M22 * right.M22) + (left.M23 * right.M32) + (left.M24 * right.M42);
            temp.M23 = (left.M21 * right.M13) + (left.M22 * right.M23) + (left.M23 * right.M33) + (left.M24 * right.M43);
            temp.M24 = (left.M21 * right.M14) + (left.M22 * right.M24) + (left.M23 * right.M34) + (left.M24 * right.M44);
            temp.M31 = (left.M31 * right.M11) + (left.M32 * right.M21) + (left.M33 * right.M31) + (left.M34 * right.M41);
            temp.M32 = (left.M31 * right.M12) + (left.M32 * right.M22) + (left.M33 * right.M32) + (left.M34 * right.M42);
            temp.M33 = (left.M31 * right.M13) + (left.M32 * right.M23) + (left.M33 * right.M33) + (left.M34 * right.M43);
            temp.M34 = (left.M31 * right.M14) + (left.M32 * right.M24) + (left.M33 * right.M34) + (left.M34 * right.M44);
            temp.M41 = (left.M41 * right.M11) + (left.M42 * right.M21) + (left.M43 * right.M31) + (left.M44 * right.M41);
            temp.M42 = (left.M41 * right.M12) + (left.M42 * right.M22) + (left.M43 * right.M32) + (left.M44 * right.M42);
            temp.M43 = (left.M41 * right.M13) + (left.M42 * right.M23) + (left.M43 * right.M33) + (left.M44 * right.M43);
            temp.M44 = (left.M41 * right.M14) + (left.M42 * right.M24) + (left.M43 * right.M34) + (left.M44 * right.M44);
            
            result = temp;
		}
       
		public static Matrix4 operator *(Matrix4 left, Matrix4 right)
		{
            Matrix4 result;
            Multiply(ref left, ref right, out result);
            return result;
		}
	}
}
